﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

#endregion

namespace ScrumTable.UI.View.Converter
{

    /// <summary>
    /// Converts a object to Visibility
    /// If object != null -> Visible
    /// If object == null -> Hidden
    /// </summary>
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class ConverterIsNullVisibility : IValueConverter
    {

        /// <summary>
        /// If this value is true this converter tests if the value is empty 
        /// </summary>
        public bool IsEmpty { get; set; }


        /// <summary>
        /// If this value is true this converter tests if the value is empty 
        /// </summary>
        public bool Invert { get; set; }

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        /// <summary>
        /// Converts any object to Visibility. If the value is null it returns Visibility.Collapsed. Else Visibility.Visible.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// If the value is null => Visibility.Collapsed.
        /// Else Visibility.Visible.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility result = Visibility.Visible;
            if (value == null || (IsEmpty && string.IsNullOrEmpty(value.ToString())))
                result = Visibility.Collapsed;
            
            if(Invert) 
            {
                result = result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
            }
            return result;
        }

        /// <summary>
        /// Not supported. Throws NotSupportedException.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();

        }
       
        #endregion


       
    }
}
